package com.gardenguell.salad.task;

import android.os.AsyncTask;
import com.gardenguell.salad.app.HandlerCenter;
import com.gardenguell.salad.app.Setting;
import com.gardenguell.salad.helper.SimpleWifiHelper;
import com.gardenguell.salad.app.HandlerCenter.Flag;
import com.gardenguell.salad.app.HandlerCenter.Message;
import com.gardenguell.salad.util.WifiUtil;

/**
 * Created by absurd on 2015/11/26.
 */
public class ConfigWifiTask extends AsyncTask<String, Void, Boolean> {

    private SimpleWifiHelper mHelper;
    private HandlerCenter mHandlerCenter = HandlerCenter.getInstance();
    private String mTip;


    public ConfigWifiTask(SimpleWifiHelper helper){
        this.mHelper = helper;
    }

    public ConfigWifiTask(SimpleWifiHelper helper, String tip){
        this.mHelper = helper;
        this.mTip = tip;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        android.os.Message msg = android.os.Message.obtain();
        msg.what = Message.SHOW_PROGRESS;
        if(mTip != null && !mTip.isEmpty())
            msg.obj = mTip;
        mHandlerCenter.getHandler(Flag.BASE_ACTIVITY).sendMessage(msg);
    }

    @Override
    protected Boolean doInBackground(String... params) {
        if(params != null && params.length > 0){
            if(params[0].equals(Command.FIND)){
                String res = mHelper.findSimpleWifi();
                if(res != null && !res.isEmpty())
                    return true;
            }else if(params[0].equals(Command.NAME)){
                String res = mHelper.findModuleName();
                if(res != null && !res.isEmpty())
                    return true;
            }
            else if(params[0].equals(Command.CONFIG)){
                if(params.length == 3) {
                    boolean res = mHelper.configureModule(params[1], params[2]);
                    return res;
                }
            }else if(params[0].equals(Command.CONNECT)){
                if(params.length == 3) {
                    WifiUtil.getInstance().connectWifi(params[1], params[2]);
                    try {
                        Thread.sleep(15000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return true;
                }
            }else if(params[0].equals(Command.MAC)){
                String mac = mHelper.findModuleMac();
                if(mac != null && !mac.isEmpty())
                    return true;
            }else if(params[0].equals(Command.CONFIG_SERVER_INFO)){
                return mHelper.configureServerInfo(Setting.hostIp, Setting.remotePort, Setting.localPort);
            }
        }
        return false;
    }


    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        mHandlerCenter.getHandler(Flag.BASE_ACTIVITY).sendEmptyMessage(Message.DISMISS_PROGRESS);

    }

    public interface Command{
        String FIND = "find";
        String CONFIG = "config";
        String CONNECT = "connect";
        String NAME = "name";
        String MAC = "mac";
        String CONFIG_SERVER_INFO = "config_server_info";
    }
}
